package com.mt.mtyx.acl.controller;

import com.mt.mtyx.acl.service.PermissionService;
import com.mt.mtyx.common.exception.BusinessException;
import com.mt.mtyx.common.result.Result;
import com.mt.mtyx.common.utils.Helper;
import com.mt.mtyx.entity.acl.Permission;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import java.util.List;
import java.util.Map;

/**
 * @author WMT
 * @Date 2023/7/17 11:43
 */
@Api(tags = "权限管理")
@RequestMapping("/admin/acl/permission")
@RestController
public class PermissionController {

    @Autowired
    private PermissionService permissionService;

    @ApiOperation("获取菜单列表")
    @GetMapping
    public Result<List<Permission>> getPermissionList() throws Exception {
        return Result.success(permissionService.getAllPermission());
    }

    @ApiOperation("添加菜单")
    @PostMapping("/save")
    public Result<Integer> save(@RequestBody Permission permission) throws Exception {
        return Result.success(permissionService.insert(permission));
    }

    @ApiOperation("修改菜单")
    @PostMapping("/update")
    public Result<Integer> update(@RequestBody Permission permission) throws Exception {
        if (StringUtils.isEmpty(permission.getId())) {
            throw new BusinessException("id不能为空！", HttpStatus.BAD_REQUEST.value());
        }
        return Result.success(permissionService.update(permission));
    }


    @ApiOperation("递归删除菜单")
    @DeleteMapping("/remove/{id}")
    public Result<Integer> remove(@PathVariable Long id) throws Exception {
        if (StringUtils.isEmpty(id)) {
            throw new BusinessException("id不能为空！", HttpStatus.BAD_REQUEST.value());
        }
        return Result.success(permissionService.removeChildById(id));
    }

    @ApiOperation("根据角色id查询所有权限")
    @GetMapping("/toAssign/{roleId}")
    public Result<Map<String, Object>> getPermissionByRoleId(@PathVariable Long roleId) throws Exception {
        if (StringUtils.isEmpty(roleId)) {
            throw new BusinessException("id不能为空！", HttpStatus.BAD_REQUEST.value());
        }
        return Result.success(permissionService.getPermissionByRoleId(roleId));
    }


    @ApiOperation("根据角色id设置权限")
    @PostMapping("/doAssign")
    public Result<Integer> setPermissionByRoleId(
            @RequestParam("roleId") Long roleId,
            @RequestParam("permissionIds") List<Long> permissionIds
    ) throws Exception {
        if (StringUtils.isEmpty(roleId)) {
            throw new BusinessException("角色id不能为空！", HttpStatus.BAD_REQUEST.value());
        }
        if (permissionIds == null || permissionIds.isEmpty()) {
            throw new BusinessException("权限id不能为空！", HttpStatus.BAD_REQUEST.value());
        }
        return Result.success(permissionService.setPermissionByRoleId(roleId, permissionIds));
    }
}
